package com.woniu113.junk.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.site.dto.AllNoticeDTO;
import com.woniu113.junk.site.dto.AllStationInfoDTO;
import com.woniu113.junk.site.exception.NoticeException;
import com.woniu113.junk.site.model.Notice;
import com.woniu113.junk.site.mapper.NoticeMapper;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.model.WasteType;
import com.woniu113.junk.site.param.notice.AddNoticeParam;
import com.woniu113.junk.site.param.notice.AllNoticeParam;
import com.woniu113.junk.site.param.notice.UpdateNoticeParam;
import com.woniu113.junk.site.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeService noticeService;

    @Override
    public Notice addNotice(AddNoticeParam param, Integer adminId) {
        //1.判断数据完整性
        if (param.getNoticeTitle()==null ||param.getNoticeContent()==null){
            throw new NoticeException("请将数据填写完整",6001);
        }
        //2.执行新增
        Notice notice = new Notice();
        notice.setNoticeTitle(param.getNoticeTitle());
        notice.setNoticeContent(param.getNoticeContent());
        notice.setUserId(adminId);
        notice.setNoticeState(Data.NOTICE_STATE_WAIT);
        notice.setCreateTime(LocalDateTime.now());
        noticeMapper.insert(notice);
        return notice;
    }

    @Override
    public void updateNotice(UpdateNoticeParam param, Integer adminId) {
        //1.判断数据完整性
        if (param.getNoticeId()==null ||param.getNoticeContent()==null ||param.getNoticeTitle()==null){
            throw new NoticeException("请将数据填写完整",6001);
        }
        //2.判断该公告id是否存在公告数据(要求状态为待发布(待发布才可以进行修改))
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id",param.getNoticeId()).eq("notice_state",Data.NOTICE_STATE_WAIT);
        Notice notice = noticeMapper.selectOne(queryWrapper);
        if (notice==null){
            throw new NoticeException("不存在能修改的公告信息",6002);
        }
        //3.执行修改操作
        notice.setNoticeTitle(param.getNoticeTitle());
        notice.setNoticeContent(param.getNoticeContent());
        notice.setUserId(adminId);
        notice.setCreateTime(LocalDateTime.now());
        noticeService.save(notice);
    }

    @Override
    public void upNotice(Integer noticeId) {
        //1.判断数据完整性
        if (noticeId==null){
            throw new NoticeException("请将数据填写完整",6001);
        }
        //2.判断该公告能否发布
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id",noticeId).eq("notice_state", Data.NOTICE_STATE_WAIT);
        Notice notice = noticeMapper.selectOne(queryWrapper);
        if (notice==null){
            throw new NoticeException("该公告不能发布",6003);
        }
        //3.发布公告(执行修改操作,修改状态)
        notice.setPublishTime(LocalDateTime.now());
        notice.setNoticeState(Data.NOTICE_STATE_UP);
        noticeService.updateById(notice);
    }

    @Override
    public void deleteNotice(Integer noticeId) {
        //1.判断数据完整性
        if (noticeId==null){
            throw new NoticeException("请将数据填写完整",6001);
        }
        //2.判断该公告能否删除
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id",noticeId).ne("notice_state", Data.NOTICE_STATE_DELETE);
        Notice notice = noticeMapper.selectOne(queryWrapper);
        if (notice==null){
            throw new NoticeException("该公告已经被删除,无法再次删除",6004);
        }
        //3.删除公告(执行修改操作,修改状态)
        notice.setNoticeState(Data.NOTICE_STATE_DELETE);
        noticeService.updateById(notice);
    }

    @Override
    public AllNoticeDTO findAll(AllNoticeParam param, Integer offset) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (param.getNoticeState()!=null){
            queryWrapper.eq("notice_state",param.getNoticeState());
        }
        if (param.getNoticeTitle()!=null && !param.getNoticeTitle().equals("")){
            queryWrapper.eq("notice_title",param.getNoticeTitle());
        }
        int size =Math.toIntExact(noticeMapper.selectCount(queryWrapper));
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<Notice> infos = noticeMapper.selectList(queryWrapper);
        AllNoticeDTO dto = new AllNoticeDTO();
        dto.setList(infos);
        dto.setTotal(size);
        return dto;
    }
}
